package handel

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-login-kit/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	if title == "" || content == "" || img == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	data := model.Article{
		Title:   title,
		Content: content,
		Img:     img,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var article []model.Article
	model.DB.Offset(offset).Limit(size).Find(&article)
	var total int64
	model.DB.Model(model.Article{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"total": total,
		"data":  article,
	})
}
